package com.ihr360.job.core;

import com.ihr360.job.core.scope.context.ChunkContext;

/**
 * Listener interface for the lifecycle of a chunk.  A chunk
 * can be through of as a collection of items that will be
 * committed together.
 *
 * @author Lucas Ward
 * @author Michael Minella
 *
 */
public interface ChunkListener extends StepListener {

    static final String ROLLBACK_EXCEPTION_KEY = "sb_rollback_exception";

    /**
     * Callback before the chunk is executed, but inside the transaction.
     *
     * @param context The current {@link ChunkContext}
     */
    void beforeChunk(ChunkContext context);

    /**
     * Callback after the chunk is executed, outside the transaction.
     *
     * @param context The current {@link ChunkContext}
     */
    void afterChunk(ChunkContext context);

    /**
     * Callback after a chunk has been marked for rollback.  It is invoked
     * after transaction rollback.  While the rollback will have occurred,
     * transactional resources might still be active and accessible.  Due to
     * this, data access code within this callback will still "participate" in
     * the original transaction unless it declares that it run in its own
     * transaction.  Hence: <em> Use PROPAGATION_REQUIRES_NEW for any
     * transactional operation that is called from here.</em>
     *
     * @param context the chunk context containing the exception that caused
     * the underlying rollback.
     */
    void afterChunkError(ChunkContext context);
}
